Opening An Image¶

In [1]:
import cv2
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import cv2

ModuleNotFoundError: No module named 'cv2'
In [2]:
install cv2
  Input In [2]
    install cv2
            ^
SyntaxError: invalid syntax
In [3]:
import cv2
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 import cv2

ModuleNotFoundError: No module named 'cv2'
In [3]:
import cv2
In [4]:
from matplotlib import pyplot as plt
In [6]:
image_file="C:\\Users\\sharp25\\Downloads\\Tanuja Kothapalli _ DL Copy.jpg"
img=cv2.imread(image_file)
In [17]:
def display(im_path):
    dpi = 80
    im_data = plt.imread(im_path)

    height, width  = im_data.shape[:2]
    
    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])

    # Hide spines, ticks, etc.
    ax.axis('off')

    # Display the image.
    ax.imshow(im_data, cmap='hsv')

    plt.show()
In [18]:
display(image_file)

inverted image¶

In [8]:
inverted_image = cv2.bitwise_not(img)
cv2.imwrite("C:/Users/sharp25/Downloads/inverted.jpg", inverted_image)
Out[8]:
True
In [9]:
display("C:/Users/sharp25/Downloads/inverted.jpg")

Rescaling¶

In [ ]:
 
In [ ]:
 

Binarization¶

In [10]:
def grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
In [11]:
gray_image = grayscale(img)
cv2.imwrite("C:/Users/sharp25/Downloads/gray.jpg", gray_image)
Out[11]:
True
In [12]:
display("C:/Users/sharp25/Downloads/gray.jpg")
In [37]:
thresh, im_bw = cv2.threshold(gray_image, 100, 300, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/sharp25/Downloads/bw_image.jpg", im_bw)
Out[37]:
True
In [38]:
display("C:/Users/sharp25/Downloads/bw_image.jpg")

Noise Removal¶

In [39]:
def noise_removal(image):
    import numpy as np
    kernel = np.ones((1, 1), np.uint8)
    image = cv2.dilate(image, kernel, iterations=1)
    kernel = np.ones((1, 1), np.uint8)
    image = cv2.erode(image, kernel, iterations=1)
    image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
    image = cv2.medianBlur(image, 3)
    return (image)
In [40]:
no_noise = noise_removal(im_bw)
cv2.imwrite("C:/Users/sharp25/Downloads/no_noise.jpg", no_noise)
Out[40]:
True
In [42]:
display("C:/Users/sharp25/Downloads/no_noise.jpg")

Dilation and Erosion¶

In [43]:
def thin_font(image):
    import numpy as np
    image = cv2.bitwise_not(image)
    kernel = np.ones((2,2),np.uint8)
    image = cv2.erode(image, kernel, iterations=1)
    image = cv2.bitwise_not(image)
    return (image)
In [44]:
eroded_image = thin_font(no_noise)
cv2.imwrite("C:/Users/sharp25/Downloads/eroded_image.jpg", eroded_image)
Out[44]:
True
In [45]:
display("C:/Users/sharp25/Downloads/eroded_image.jpg")
In [49]:
pip install pytesseract
Collecting pytesseract
  Downloading pytesseract-0.3.9-py2.py3-none-any.whl (14 kB)
Requirement already satisfied: Pillow>=8.0.0 in c:\users\sharp25\anaconda3\lib\site-packages (from pytesseract) (9.0.1)
Requirement already satisfied: packaging>=21.3 in c:\users\sharp25\anaconda3\lib\site-packages (from pytesseract) (21.3)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\sharp25\anaconda3\lib\site-packages (from packaging>=21.3->pytesseract) (3.0.4)
Installing collected packages: pytesseract
Successfully installed pytesseract-0.3.9
Note: you may need to restart the kernel to use updated packages.
In [7]:
import pytesseract
from PIL import Image
In [11]:
img_file="C:/Users/sharp25/OneDrive/Desktop/ocr/PragnaGundala_DL.jpg"
In [13]:
img=Image.open(img_file)
In [14]:
ocr_result =pytesseract.image_to_string(img)
In [15]:
print(ocr_result)
FEDERAL LIMITS APPLY
4d eo

1GUNDAI
SPRAGNA

3pos 02/09/1989 —_—aaiss 12/07/2019
8 14660 NE 31ST ST APT C306 a
BELLEVUE WA 98007-7641

45 SEX F 18 EYES BLK
16 HGT 5'-01" 47WGT 164

& wexe 02/0

 

5DD WDL7TST77' '207193A1354
. REV 11/12/2019

 


Preprocessing image for OCR¶

  • Inverted Images
  • Rescaling
  • Binarization
  • Noise Removal
  • Dilation and Erosion
  • Rotation / Deskewing
  • Removing Borders
  • Missing Borders
  • Transparency / Alpha Channel

OPENING AN IMAGE¶

In [18]:
import cv2
from matplotlib import pyplot as plt
image_file = "C:/Users/sharp25/OneDrive/Desktop/ocr/img.jpg"
img = cv2.imread(image_file)
In [19]:
def display(im_path):
    dpi = 80
    im_data = plt.imread(im_path)

    height, width  = im_data.shape[:2]
    
    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])

    # Hide spines, ticks, etc.
    ax.axis('off')

    # Display the image.
    ax.imshow(im_data, cmap='gray')

    plt.show()
In [20]:
display(image_file)

Inverted Image¶

In [22]:
inverted_image = cv2.bitwise_not(img)
cv2.imwrite('C:/Users/sharp25/OneDrive/Desktop/ocr/inverted.jpg',inverted_image)
Out[22]:
True
In [23]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/inverted.jpg")

Binarization¶

In [27]:
def grayscale(image):
    return cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
In [28]:
gray_image = grayscale(img)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/gray.jpg", gray_image)
Out[28]:
True
In [29]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/gray.jpg")
In [30]:
thresh, im_bw = cv2.threshold(gray_image, 210, 230, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/bw_image.jpg", im_bw)
Out[30]:
True
In [31]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/bw_image.jpg")

Noise Removal¶

In [36]:
def noise_removal(image):
    import numpy as np
    kernel = np.ones((1, 1), np.uint8)
    image = cv2.dilate(image, kernel, iterations=1)
    kernel = np.ones((1, 1), np.uint8)
    image = cv2.erode(image, kernel, iterations=1)
    image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
    image = cv2.medianBlur(image, 3)
    return (image)
In [38]:
no_noise = noise_removal(im_bw)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_noise.jpg", no_noise)
Out[38]:
True
In [40]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/no_noise.jpg")

Dilation and Erosion¶

In [41]:
def thin_font(image):
    import numpy as np
    image = cv2.bitwise_not(image)
    kernel = np.ones((2,2),np.uint8)
    image = cv2.erode(image, kernel, iterations=1)
    image = cv2.bitwise_not(image)
    return (image)
In [42]:
eroded_image = thin_font(no_noise)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/eroded_image.jpg", eroded_image)
Out[42]:
True
In [44]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/eroded_image.jpg")
In [72]:
def thick_font(image):
    import numpy as np
    image = cv2.bitwise_not(image)
    kernel = np.ones((1,1),np.uint8)
    image = cv2.dilate(image, kernel, iterations=1)
    image = cv2.bitwise_not(image)
    return (image)
In [73]:
dilated_image = thick_font(no_noise)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/dilated_image.jpg", dilated_image)
Out[73]:
True
In [74]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/dilated_image.jpg")

Rotation / Deskewing¶

In [63]:
new = cv2.imread("C:/Users/sharp25/OneDrive/Desktop/ocr/hqdefault.jpg")
display("C:/Users/sharp25/OneDrive/Desktop/ocr/hqdefault.jpg")
In [98]:
#https://becominghuman.ai/how-to-automatically-deskew-straighten-a-text-image-using-opencv-a0c30aed83df
import numpy as np

def getSkewAngle(cvImage) -> float:
    # Prep image, copy, convert to gray scale, blur, and threshold
    newImage = cvImage.copy()
    gray = cv2.cvtColor(newImage, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (9, 9), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Apply dilate to merge text into meaningful lines/paragraphs.
    # Use larger kernel on X axis to merge characters into single line, cancelling out any spaces.
    # But use smaller kernel on Y axis to separate between different blocks of text
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
    dilate = cv2.dilate(thresh, kernel, iterations=1)

    # Find all contours
    contours, hierarchy = cv2.findContours(dilate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key = cv2.contourArea, reverse = True)
    for c in contours:
        rect = cv2.boundingRect(c)
        x,y,w,h = rect
        cv2.rectangle(newImage,(x,y),(x+w,y+h),(0,255,0),2)

    # Find largest contour and surround in min area box
    largestContour = contours[0]
    print (len(contours))
    minAreaRect = cv2.minAreaRect(largestContour)
    cv2.imwrite("temp/boxes.jpg", newImage)
    # Determine the angle. Convert it to the value that was originally used to obtain skewed image
    angle = minAreaRect[-1]
    if angle < -45:
        angle = 90 + angle
    return -1.0 * angle
# Rotate the image around its center
def rotateImage(cvImage, angle: float):
    newImage = cvImage.copy()
    (h, w) = newImage.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
    return newImage
In [99]:
# Deskew image
def deskew(cvImage):
    angle = getSkewAngle(cvImage)
    return rotateImage(cvImage, -1.0 * angle)
In [100]:
fixed = deskew(new)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg", fixed)
9
Out[100]:
True
In [101]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg")

Removing Borders¶

In [102]:
display("C:/Users/sharp25/OneDrive/Desktop/ocr/rotated_fixed.jpg")
In [103]:
def remove_borders(image):
    contours, heiarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cntsSorted = sorted(contours, key=lambda x:cv2.contourArea(x))
    cnt = cntsSorted[-1]
    x, y, w, h = cv2.boundingRect(cnt)
    crop = image[y:y+h, x:x+w]
    return (crop)
In [104]:
no_borders = remove_borders(rotated_fixed)
cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg", no_borders)
display('C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [104], in <cell line: 1>()
----> 1 no_borders = remove_borders(rotated_fixed)
      2 cv2.imwrite("C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg", no_borders)
      3 display('C:/Users/sharp25/OneDrive/Desktop/ocr/no_borders.jpg')

NameError: name 'rotated_fixed' is not defined
In [ ]: